package com.talkyun.openx.interceptor;

import com.talkyun.openx.server.core.AbstractInterceptor;
import com.talkyun.openx.server.core.ServiceRequest;
import com.talkyun.openx.server.core.ServiceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingInterceptor extends AbstractInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);

    @Override
    public void doMessageIn(ServiceRequest sr) {
        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("session:").append(sr.getSession()).append(",");
            sb.append("mapping:").append(sr.getMapping()).append(", ");
            sb.append("method:").append(sr.getMethod());
            if (sr.getArgs() != null) {
                sb.append("(").append(sr.getArgs()).append(")");
            } else {
                sb.append("()");
            }
            logger.debug(sb.toString());
        }
    }

    @Override
    public void doMessageOut(ServiceResponse sp) {
        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("session:").append(sp.getSession()).append(",");
            sb.append("status:").append(sp.getStatus()).append(",");
            sb.append("result:").append(sp.getResult());
            logger.debug(sb.toString());
        }
    }

    @Override
    protected void doException(Throwable t) {
        logger.error(t.getMessage(), t);
    }
}